package com.customization.unionpay.market.service.impl;

import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.service.MarketingPrepaidActivityLedgerService;
import com.customization.unionpay.market.util.PropUtils;
import com.engine.core.impl.Service;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

public class MarketingPrepaidActivityLedgerServiceImpl extends Service implements MarketingPrepaidActivityLedgerService {

    private static final Logger log = LoggerFactory.getLogger(MarketingPrepaidActivityLedgerServiceImpl.class);

    @Override
    public String marketFinancialConcluded(String ids, User user) throws Exception {
        ids = ids.replace(",","','");
        log.info("ids:"+ids);
        String result = null;
        RecordSet rs = new RecordSet();
        try {
            // 营销预付活动台账表单id
            int marketingPrepaidActivityLedgerFormId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_prepaid_activity_ledger_form_id"));
            String isYes = com.customization.unionpay.market.util.FormDataUtils.getItemValue(marketingPrepaidActivityLedgerFormId, "cwsfjs", "是");
            String isNot = com.customization.unionpay.market.util.FormDataUtils.getItemValue(marketingPrepaidActivityLedgerFormId, "cwsfjs", "否");

            // 营销活动事项台账表单id
            int formId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_ledger_form_id"));
            String tablename = com.customization.unionpay.market.util.FormDataUtils.getTableName(formId);
            log.info("营销活动事项台账表单："+tablename);

            String selectValueYes = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "hdsfhx", "是");
            log.info("活动是否核销 是："+selectValueYes);

            String maintablename = com.customization.unionpay.market.util.FormDataUtils.getTableName(marketingPrepaidActivityLedgerFormId);
            log.info("maintablename："+maintablename);

            String sql = "select id,yfkjy,yxpthdsqm,cwsfjs from #{tableName} where id in ('"+ids+"') ";
            sql = sql.replace("#{tableName}",maintablename);
            log.info("查询sql:"+sql);

            rs.executeQuery(sql);
            while (rs.next()){
                String cwsfjs = Util.null2String(rs.getString("cwsfjs"));
                log.info("cwsfjs:"+cwsfjs);
                String yxpthdsqm = Util.null2String(rs.getString("yxpthdsqm"));
                log.info("yxpthdsqm:"+yxpthdsqm);
                if(!isYes.equals(cwsfjs)){
                    String id = Util.null2String(rs.getString("id"));
                    String yfkjy = Util.null2String(rs.getString("yfkjy"));
                    // ①营销预付款已完成核销和退回， 【预付款结余】=0时
                    log.info("yfkjy.trim():"+yfkjy.trim());
                    if("0".equals(yfkjy) || "0.0".equals(yfkjy) || "0.00".equals(yfkjy)){
                        if(!("").equals(yxpthdsqm)) {
                            boolean b = FormDataUtils.selectSfhx(tablename, yxpthdsqm, selectValueYes);
                            if (b) {
                                updateCwsfjsById(id, isYes, maintablename);
                            } else {
                                if (result == null) {
                                    result = "";
                                    result += "营销预付活动台账中营销平台活动授权码为：" + yxpthdsqm + "的活动未核销！";
                                } else {
                                    result += ",营销预付活动台账中营销平台活动授权码为：" + yxpthdsqm + "的活动未核销！";
                                }
                            }
                        }else {
                            if (result == null) {
                                result = "";
                                result += "营销预付活动台账中营销平台活动授权码为空！";
                            } else {
                                result += ",营销预付活动台账中营销平台活动授权码为空！";
                            }
                        }
                    }else {
                        if(result == null){
                            result = "";
                            result += "营销预付活动台账中营销平台活动授权码为："+yxpthdsqm+"的营销预付款核销和退回未完成！";
                        }else {
                            result += ",营销预付活动台账中营销平台活动授权码为："+yxpthdsqm+"的营销预付款核销和退回未完成！";
                        }
                    }
                }else {
                    if(result == null){
                        result = "";
                        result += "营销预付活动台账中营销平台活动授权码为："+yxpthdsqm+"的财务事项已结束！";
                    }else {
                        result += ",营销预付活动台账中营销平台活动授权码为："+yxpthdsqm+"的财务事项已结束！";
                    }
                }
            }
            return result;
        } catch (Exception e) {
            throw new Exception(e.getMessage());
        }
    }

    /**
     * 修改营销预付活动台账通过请id
     * @param id                          请款批次号
     * @param cwsfjs                      财务事项是否结束
     * @param maintablename
     * @return
     */
    private void updateCwsfjsById(String id,String cwsfjs,String maintablename) {
        RecordSetTrans rst = new RecordSetTrans();
        // 开启事务（关闭自动提交）
        rst.setAutoCommit(false);
        try {
            String updateSql = "update #{tableName} set cwsfjs=? where id=? ";
            updateSql = updateSql.replace("#{tableName}",maintablename);
            log.info("updateSql:"+updateSql);
            rst.executeUpdate(updateSql,cwsfjs,id);
            // 提交事务
            rst.commit();
        } catch (Exception e) {
            // 回滚
            rst.rollback();
            log.error("修改营销预付活动台账异常："+e.getMessage());
        }
    }
}
